package com.yuexunit.operationman.nbsy.module.plant.presenter.impl;

import com.yuexunit.operationman.nbsy.base.SubmitBean;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.plant.model.impl.MaterialAddModel;
import com.yuexunit.operationman.nbsy.module.plant.presenter.IMaterialAddPresenter;
import com.yuexunit.operationman.nbsy.module.plant.ui.view.IMaterialAddView;

import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * Created by chenss on 2017/7/21 0021.
 */

public class MaterialAddPresenter implements IMaterialAddPresenter {

    private MaterialAddModel mModel;
    private IMaterialAddView mView;

    public MaterialAddPresenter(IMaterialAddView view, MaterialAddModel model) {
        mModel = model;
        mView = view;
    }

    @Override
    public void insertMaterial(Map<String, String> praise) {
        mModel.addMaterial(praise)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        mView.loadingDialogStatus(AppConfig.SHOW_DIALOG);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<SubmitBean>() {
                    @Override
                    public void onCompleted() {
                        mView.loadingDialogStatus(AppConfig.DISMISS_DIALOG);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.loadingDialogStatus(AppConfig.DISMISS_DIALOG);
                    }

                    @Override
                    public void onNext(SubmitBean submitBean) {
                        mView.insertMaterial(submitBean);
                    }
                });
    }
}
